package com.hurui.xiaoming.activity.manager;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.adapter.ManagerAdapter;
import com.hurui.xiaoming.bean.ManagerBean;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClientManagerActivity extends BaseActivity   {
    private TextView mOut_login;
    private XRecyclerView mLv;
    private Map<String ,Object> map;
    private ManagerAdapter managerAdapter;
    private int page=0;
    private List<ManagerBean.DataBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_manager);
        tintManager.setStatusBarTintColor(R.color.systembartintColor2);
        initView();
        getData();
        initAction();

        CheckPerMission();
    }

    @Override
    public void initView() {
        mOut_login = (TextView) findViewById(R.id.out_login);
        mLv = (XRecyclerView) findViewById(R.id.lv);
        managerAdapter = new ManagerAdapter(this);
        mLv.setLayoutManager(new LinearLayoutManager(this));
        mLv.setAdapter(managerAdapter);
        map=new HashMap<>();

    }

    @Override
    public void initAction() {

        mOut_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Out_login();
            }
        });
        mLv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=0;
                getData();
            }

            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    private void Out_login(){
        //保存用户上次登录时间
        WriteReadSharedPrefs.outUser(mContext);
        apiSevice.onHttp_outLogin().enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                onFinish();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                onFinish();
            }
        });
    }

    public void getData() {

        map.put("ui_id",WriteReadSharedPrefs.readUserID(this)+"");//WriteReadSharedPrefs.readUserID(this)+""
        map.put("ui_type",WriteReadSharedPrefs.readUserUI_Type(this));//
        map.put("page_num",page);
        apiSevice.onHttp_manager(map).enqueue(new Callback<ManagerBean>() {
            @Override
            public void onResponse(Call<ManagerBean> call, Response<ManagerBean> response) {
                if (response!=null){
                    if (response.body().getStatus().equals("sucess")){
                        List<ManagerBean.DataBean> data = response.body().getData();
                        managerAdapter.refresh(data);
                    }
                }
            }

            @Override
            public void onFailure(Call<ManagerBean> call, Throwable t) {

            }
        });
    }

    /**
     * 6.0的权限监测
     */
    public static final int READ_EXTERNAL_STORAGE_CODE = 7;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.CALL_PHONE};

    /**
     * 权限检查
     * @param
     */
    public void CheckPerMission(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(this,PERMISSIONS_STORAGE,
                    READ_EXTERNAL_STORAGE_CODE);
        }
    }

    /**
     * 权限申请的回调
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doNext(requestCode,grantResults);
    }

    /**
     * 权限的处理
     * @param requestCode
     * @param grantResults
     */
    private void doNext(int requestCode, int[] grantResults) {
        if (requestCode == READ_EXTERNAL_STORAGE_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted     同意

            } else {
                // Permission Denied       拒绝
                new AlertDialog.Builder(ClientManagerActivity.this)
                        .setTitle("权限申请")
                        .setMessage("在设置-应用-权限中开始读写权限，以保证功能的正常使用")
                        .setPositiveButton("去开启", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);

                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .setCancelable(false)
                        .show();
            }
        }
    }

}
